package com.wejoy.video.middle.station.admin.controller.overtime;

import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.overtime.OvertimeInfo;
import com.wejoy.video.middle.station.data.domain.system.SysDept;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.service.overtime.IOvertimeInfoService;
import com.wejoy.video.middle.station.data.util.StringUtils;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.evsapi.DeparmentInfoListVO;
import com.wejoy.video.middle.station.data.vo.overtime.OverTimeSettingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 加班详情
 *
 * @author miracle_j
 * @date 2024年04月22日 11:11:52
 */
@RestController
@RequestMapping("/overtime/overtimeInfo")
public class OvertimeInfoController extends BaseController {

    @Autowired
    private IOvertimeInfoService overtimeInfoService;

    /**
     * 保存或更新
     *
     * @param overtimeInfoReq overtimeInfoReq
     * @return RespJSON
     */
    @PostMapping("/save")
    public RespJSON save(@RequestBody OvertimeInfo overtimeInfoReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        overtimeInfoService.save(overtimeInfoReq, loginSysUser);
        return RespJSON.success();
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(overtimeInfoService.getOneByDeptId(id));
    }


    @PostMapping("getMydeptList")
    @RoleCheck(name = "overTimeSetting")
    public RespJSON getMydeptList(@RequestBody SysDept sysDept, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        List<DeparmentInfoListVO> departmentInfoList = loginSysUser.getDepartmentInfoList();
        List<OverTimeSettingVO> overTimeSettingVOS = new ArrayList<>();
        if(!CollectionUtils.isEmpty(departmentInfoList)){
            if(!StringUtils.isEmpty(sysDept.getName())){
                departmentInfoList = departmentInfoList.stream().filter(item -> item.getName().contains(sysDept.getName())).collect(Collectors.toList());
            }
            departmentInfoList.forEach(item->{
                OvertimeInfo overtimeInfo = overtimeInfoService.getOneByDeptId(item.getDepartmentId());
                OverTimeSettingVO overTimeSettingVO = new OverTimeSettingVO();
                overTimeSettingVO.setName(item.getName());
                overTimeSettingVO.setDeptId(item.getDepartmentId());
                if(!ObjectUtils.isEmpty(overtimeInfo)){
                    overTimeSettingVO.setDataStatus(overtimeInfo.getDataStatus());
                    overTimeSettingVO.setBeginTime(overtimeInfo.getBeginTime());
                    overTimeSettingVO.setEndTime(overtimeInfo.getEndTime());
                }
                overTimeSettingVO.setStatus(overtimeInfo==null?0:overtimeInfo.getStatus());
                overTimeSettingVOS.add(overTimeSettingVO);
            });
        }
        PageResult result = new PageResult(sysDept.getPageNo(), sysDept.getPageSize(), overTimeSettingVOS.size(), overTimeSettingVOS);
        return RespJSON.success(result);
    }

}
